package com.zut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.zut.entity.OrderInfo;
import com.zut.entity.PaymentInfo;
import com.zut.entity.Product;
import com.zut.enums.OrderStatus;
import com.zut.enums.PayType;
import com.zut.mapper.OrderInfoMapper;
import com.zut.mapper.ProductMapper;
import com.zut.service.OrderInfoService;
import com.zut.util.OrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper,OrderInfo> implements OrderInfoService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public OrderInfo createOrderByProductId(Long productId) {

        // 防止重复下单  查找已经存在，但是未支付的订单
        // 如果存在，直接返回订单信息即可   后面的代码不再执行
        OrderInfo orderInfo = this.getPayOrderByProductId(productId);
        if(orderInfo != null){
            return orderInfo;
        }


        // 根据 productId 获取商品信息
        Product product = productMapper.selectById(productId);
            // 创建订单
            orderInfo = new OrderInfo();
            orderInfo.setTitle(product.getTitle());
            orderInfo.setOrderNo(OrderNoUtils.getOrderNo());
            orderInfo.setProductId(productId);
            orderInfo.setTotalFee(product.getPrice());
            orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());  // 未支付

        // 把订单信息保存到数据库当中
        baseMapper.insert(orderInfo);
        return orderInfo;
    }

    // 查询已存在的但是未支付的订单
    private OrderInfo getPayOrderByProductId(Long productId) {
        // 按条件查询 需要封装条件
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 支付二维码放到数据库
     * @param orderNo
     * @param codeUrl
     */
    @Override
    public void saveCodeUrl(String orderNo, String codeUrl) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCodeUrl(codeUrl);
        // 带条件的更新
        baseMapper.update(orderInfo,queryWrapper);
    }

    // 根据订单创建时间，倒叙查询出订单
    @Override
    public List<OrderInfo> listOrderByCreateTimeDesc() {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return baseMapper.selectList(queryWrapper);
    }

    // 根据订单编号  更新订单状态
    @Override
    public void updateStatusByOrderNo(Object orderNo, OrderStatus orderStatus) {
        log.info("修改订单状态 orderNo:{},orderStatus:{}",orderNo,orderStatus);

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus(orderStatus.getType());
        baseMapper.update(orderInfo,queryWrapper);
    }

    // 获取订单状态
    @Override
    public String getOrderStatus(Object orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        return (orderInfo==null?null:orderInfo.getOrderStatus());
    }
}
